home *** CD-ROM | disk | FTP | other *** search
/ USA Bestseller / USA BESTSELLER Vol 1-95 (Hepp-Computer)(1995).iso / e206 / mkbin.c < prev    next >
Text File  |  1994-08-24  |  2KB  |  71 lines

  1. /*------------------------------------------------------------------------------
  2. **
  3. **      MKBIN.C     -     example use of C data def.
  4. **
  5. **      This program demonstrates the use of a data definition created
  6. **      by XED. The header "colours.hex" is used to rebuild an original
  7. **      executable binary file. Note the use of the PARAGRAPHS, PARAGRAPH,
  8. **      FILESIZE and BYTES_ON_LAST definitions found in colours.hex.
  9. **
  10. **
  11. **      To compile:
  12. **                      cl mkbin.c   (needs colours.hex)
  13. **
  14. **      Execute:        mkbin
  15. **
  16. **      The file 'colours.com' should be generated.
  17. **
  18. **----------------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <sys\types.h>
  22. #include <sys\stat.h>
  23.  
  24. #include "colours.hex"              // XED generated
  25.  
  26. const char *target = "colours.com";
  27.  
  28. int main ()
  29. {
  30.     int fd;
  31.     unsigned long l, wrbytes = 0L;
  32.  
  33.     if ((fd = open (target, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
  34.         _S_IREAD | _S_IWRITE)) < 0)
  35.     {
  36.         fprintf (stderr, "can\'t open %s for writing\n", target);
  37.         exit (1);
  38.     }
  39.  
  40.     for (l = 0L; l < PARAGRAPHS; l++)
  41.     {
  42.         int bytestowrite = 16, byteswritten = 0;
  43.  
  44. #ifdef BYTES_ON_LAST
  45.         if (l == PARAGRAPHS - 1L)
  46.             bytestowrite = BYTES_ON_LAST;
  47. #endif
  48.  
  49.         if ((byteswritten = write(fd, untitled[l].paragraph, bytestowrite)) < 0)
  50.         {
  51.             fprintf (stderr, "error writing %s\n", target);
  52.             close (fd);
  53.             exit (2);
  54.         }
  55.         wrbytes += byteswritten;
  56.     }
  57.     close (fd);
  58.  
  59.     if (wrbytes == FILESIZE)
  60.         fprintf (stderr, "%lu bytes written to %s\n", FILESIZE, target);
  61.     else
  62.     {
  63.         fprintf (stderr, "error: file sizes not consistent (%lu) %lu\n",
  64.             FILESIZE, wrbytes);
  65.         exit (3);
  66.     }
  67.     exit (0);
  68. }
  69.  
  70. /* EOF: MKBIN.C --------------------------------------------------------------*/
  71.